﻿using System.Collections.Generic;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using Shape = Microsoft.Office.Interop.PowerPoint.Shape;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    public static class ShapeFinder
    {
        /// <summary>
        /// Iterates through all shapes in the slide to locate shapes that match the naming
        /// of the media element.
        /// </summary>
        /// <param name="slide">The slide to search on.</param>
        /// <param name="mediaName">The name of the media element to locate the shapes for.</param>
        /// <returns>A list of Shapes that match the media element name and the system naming convention.</returns>
        public static IList<Shape> FindShapesByMediaName(Slide slide, string mediaName)
        {
            IList<Shape> list = new List<Shape>();
            foreach (Shape shape in slide.Shapes)
            {
                if (shape.Type == MsoShapeType.msoTextBox && MatchName(shape.Name, mediaName))
                {
                    list.Add(shape);
                }
            }
            return list;
        }

        /// <summary>
        /// Finds textbox Shapse based on the CaptionMediaId
        /// </summary>
        /// <param name="slide">Slide that contains the shapes.</param>
        /// <param name="mediaId">MediaId to find the shapes with.</param>
        /// <returns>Returns list of shapes that contain the mediaId</returns>
        public static IList<Shape> FindShapesByMediaId(Slide slide, string mediaId)
        {
            IList<Shape> list = new List<Shape>();
            foreach (Shape shape in slide.Shapes)
            {
                if (shape.Type == MsoShapeType.msoTextBox && shape.Tags[Constants.CaptionMediaId].Equals(mediaId))
                {
                    list.Add(shape);
                }
            }
            return list;
        }

        /// <summary>
        /// Matches the source name to the media name
        /// </summary>
        /// <param name="sourceName"></param>
        /// <param name="mediaName"></param>
        /// <returns></returns>
        public static bool MatchName(string sourceName, string mediaName)
        {
            //todo might become more complex
            return sourceName.StartsWith(mediaName);
        }
    }
}